package com.example.shanguigu.nio;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

import static com.example.shanguigu.nio.Constants.BASE_DIR;

public class NIOFileChannel01 {

    public static void main(String[] args) {
        String msg="hello nio file channel";
        try (FileOutputStream fos=new FileOutputStream(BASE_DIR+"nio_out.txt");
             FileChannel fc= fos.getChannel()){

            ByteBuffer buffer = ByteBuffer.allocate(1024);
            buffer.put(msg.getBytes(StandardCharsets.UTF_8));

            buffer.flip();

            fc.write(buffer);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
